package com.ys.nms.module.business.service.storage;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import com.ys.nms.module.business.dal.dataobject.processors.ProcessorsDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import com.ys.nms.module.business.controller.admin.storage.vo.*;
import com.ys.nms.module.business.dal.dataobject.storage.StorageDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.storage.StorageMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 存储表 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class StorageServiceImpl implements StorageService {

    @Resource
    private StorageMapper storageMapper;

    @Override
    public Integer createStorage(StorageSaveReqVO createReqVO) {
        // 插入
        StorageDO storage = BeanUtils.toBean(createReqVO, StorageDO.class);
        storageMapper.insert(storage);

        // 返回
        return storage.getStorageId();
    }

    @Override
    public void updateStorage(StorageSaveReqVO updateReqVO) {
        // 校验存在
        validateStorageExists(updateReqVO.getStorageId());
        // 更新
        StorageDO updateObj = BeanUtils.toBean(updateReqVO, StorageDO.class);
        storageMapper.updateById(updateObj);
    }

    @Override
    public void deleteStorage(Integer id) {
        // 校验存在
        validateStorageExists(id);
        // 删除
        storageMapper.deleteById(id);
    }

    @Override
        public void deleteStorageListByIds(List<Integer> ids) {
        // 删除
        storageMapper.deleteByIds(ids);
        }


    private void validateStorageExists(Integer id) {
        if (storageMapper.selectById(id) == null) {
            throw exception(STORAGE_NOT_EXISTS);
        }
    }

    @Override
    public StorageDO getStorage(Integer id) {
        return storageMapper.selectById(id);
    }

    @Override
    public PageResult<StorageDO> getStoragePage(StoragePageReqVO pageReqVO) {
        return storageMapper.selectPage(pageReqVO);
    }

    @Override
    @Slave
    @TenantIgnore
    public List<StorageDO> getStorageListByDeviceId(Integer deviceId) {
        LambdaQueryWrapperX<StorageDO> processorsWrapper = new LambdaQueryWrapperX<StorageDO>();
        processorsWrapper.eq(StorageDO::getDeviceId, deviceId);
        processorsWrapper.orderByDesc(StorageDO::getStoragePerc);
        return storageMapper.selectList(processorsWrapper);
    }



}